﻿using AuthTemplate.Infrastructure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AuthTemplate.MVC.Filter
{
    public static class ExcelResultExstion
    {
        public static void ExecuteResult<T>( this ExcelBaseResult<T> reslut, ControllerContext context)
        {
            using (MemoryStream ms = reslut.GetExcelStream())
            {
                context.HttpContext.Response.AddHeader("Content-Length", ms.Length.ToString());
                context.HttpContext.Response.ContentType = reslut.ContentType;
                context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + reslut.FullName);
                ms.Seek(0, SeekOrigin.Begin);
                Stream output = context.HttpContext.Response.OutputStream;
                byte[] bytes = new byte[1024 * 10];
                int readSize = 0;
                while ((readSize = ms.Read(bytes, 0, bytes.Length)) > 0)
                {
                    output.Write(bytes, 0, readSize);
                    context.HttpContext.Response.Flush();
                }


            }


        }
    }
}